package cn.appsys.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;




import cn.appsys.biz.AppCategoryBiz;
import cn.appsys.biz.AppInfoBiz;
import cn.appsys.biz.DataDictionaryBiz;
import cn.appsys.biz.AppVersionBiz;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.pojo.DataDictionary;
import cn.appsys.pojo.DevUser;

@Controller
public class InitController {
	@Autowired
	private AppInfoBiz appInfoBiz;

	@Autowired
	private AppCategoryBiz appCategoryBiz;

	@Autowired
	private DataDictionaryBiz dataDictionaryBiz;

	@Autowired
	private AppVersionBiz appVersionBiz;

	@RequestMapping("/index")
	public String index() {
		return "index";
	}

	@RequestMapping("/advocate")
	public String advocate() {
		return "advocate";
	}

	@RequestMapping("/addmaintain")
	public String addmaintain(Model model, int id) {
		List<AppVersion> appVersions = appVersionBiz.getByAppId(id);
		model.addAttribute("id", id);
		model.addAttribute("appVersions", appVersions);
		return "addmaintain";
	}

	
	@RequestMapping("/addmaintains")
	public String addmaintainzz(Model model, AppVersion version, HttpSession session, int id) {
		DevUser devuser = (DevUser) session.getAttribute("user");
		DevUser dd = new DevUser();
		dd.setId(devuser.getId());
		DataDictionary dataDictionary = new DataDictionary();
		dataDictionary.setValueName("3");
		AppInfo appInfo = new AppInfo();
		appInfo.setId(id);
		version.setAppInfo(appInfo);
		version.setCreatedBy(dd);
		version.setPublishStatus(dataDictionary);
		version.setCreationDate(new Date());
		appVersionBiz.addVersion(version);
		return "redirect:appmaintain";
	}
	

	
	
	
	@RequestMapping("/register")
	public String register() {
		return "register";
	}
	@RequestMapping({"/appmaintain","/adminAppList"})
	public String appmaintain(Model model,String softwareName,Integer appStatus,Integer appFlatform,
			Integer categoryLevel1,Integer categoryLevel2,Integer categoryLevel3,
			Integer page,String locationString){
		appStatus = appStatus == null ? 0 : appStatus;
		appFlatform = appFlatform == null ? 0 : appFlatform;
		categoryLevel1 = categoryLevel1 == null ? 0 : categoryLevel1;
		categoryLevel2 = categoryLevel2 == null ? 0 : categoryLevel2;
		categoryLevel3 = categoryLevel3 == null ? 0 : categoryLevel3;
		page = page == null ? 1 : page;
		int pageSize = 5;
		int pageIndex = (page - 1) * pageSize;
		List<AppInfo> appinfo = appInfoBiz.getAppInfos(softwareName, appStatus, appFlatform, categoryLevel1,
				categoryLevel2, categoryLevel3, pageIndex, pageSize);
		int row = appInfoBiz.pageCount(softwareName, appStatus, appFlatform, categoryLevel1, categoryLevel2,
				categoryLevel3);
		int pageCount = (int) Math.ceil((double) row / pageSize);
		pageCount = pageCount == 0 ? 1 : pageCount;
		model.addAttribute("appStatusList", dataDictionaryBiz.getDictionary("APP_STATUS"));
		model.addAttribute("appFlatformList", dataDictionaryBiz.getDictionary("APP_FLATFORM"));
		model.addAttribute("categoryLevel1List", appCategoryBiz.getAppCategoryByParendId(0));
		model.addAttribute("appinfo", appinfo);
		model.addAttribute("pageCount", pageCount);
		model.addAttribute("flatformValue", appFlatform);
		model.addAttribute("statusValue", appStatus);
		model.addAttribute("rows", row);
		model.addAttribute("softwareName", softwareName);
		model.addAttribute("categoryLevel1Id", categoryLevel1);
		model.addAttribute("categoryLevel2Id", categoryLevel2);
		model.addAttribute("categoryLevel3Id", categoryLevel3);
		model.addAttribute("page", page);
		return locationString;
	}

	@RequestMapping("/appindex")
	public String appindex() {
		return "appindex";
	}
	
	@RequestMapping("/adminindex")
	public String adminindex(){
		return "adminindex";
	}
	
}
